﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TGBTech.Core.Models.Models.BaoCao;
using TGBTech.Core.Models.ChungTu;
using TGBTech.Core.Models.Models.BaoCao.NhatKyChungModel;
using TGB.Core.Infrastructure;

namespace TGBTech.Core.Models.Services
{
    public class SoNhatKyChungService
    {
        //system message
        public const String MESSAGE_INVALID_DATE = "Vui lòng kiểm tra ngày tháng";
        public const String MESSAGE_INVALID_DATA = "Dữ liệu lỗi tại Data {0}";
        /// <summary>
        /// create so nhat ky chung
        /// </summary>
        /// <param name="sonhatkychung"></param>
        /// <returns>the same sonhatkychung at input ref sonhatkychung</returns>
        public SoNhatKyChung createSoNhatKyChung(DateTime ngaybatdau, DateTime ngayketthuc)
        { 
            //init sonhatkychung
            SoNhatKyChung sonhatkychung = new SoNhatKyChung();
            //init ngaybatdau, ngayketthuc
            sonhatkychung.NgayKetThuc = ngayketthuc;
            sonhatkychung.NgayBatDau = ngaybatdau;
            //check valid ngaybatdau ngayketthuc
            if (ngaybatdau == null|| ngayketthuc == null ||sonhatkychung.NgayBatDau > sonhatkychung.NgayKetThuc)
            {
                //return errormessage if dates are invalid
                sonhatkychung.Message = MESSAGE_INVALID_DATE;
                return sonhatkychung;
            }
            //check init list nhatkychung in SoNhatKyChung
            if (sonhatkychung.ListNhatKyChung == null)
            {
                sonhatkychung.ListNhatKyChung = new List<NhatKyChung>();
            }
            //search and get list data from ngaybatdau to ngayketthuc
            using (TGBTechDb dbContext = new TGBTechDb())
            {
                //get list data from DBContext with ngaybatdau and ngayketthuc
                var listData = (from e in dbContext.Data 
                                where e.NgayCT >= ngaybatdau && e.NgayCT <= ngayketthuc
                                select e).ToList(); ;
                //init object before loop
                NhatKyChung nhatkychung;
                //loop each data
                foreach (var data in listData)
                {
                    //create new object NhatKyChung with seperate No
                    nhatkychung = createNhatKyChung(data, true);
                    //add nhatkychung to sonhatkychung
                    sonhatkychung.ListNhatKyChung.Add(nhatkychung);
                    //create new object NhatKyChung with seperate Co
                    nhatkychung = createNhatKyChung(data, false);
                    //add nhatkychung to sonhatkychung
                    sonhatkychung.ListNhatKyChung.Add(nhatkychung);
                }
            }
            //return SoNhatKyChung
            return sonhatkychung;
        }

        // create nhatkychung with special data in no or co
        public static NhatKyChung createNhatKyChung(Data data, Boolean co)
        {
            //init new NhatKyChung object
            NhatKyChung nhatkychung = new NhatKyChung();
            //check valid data
            if (data == null) 
            {
                //set error message
                nhatkychung.Message = MESSAGE_INVALID_DATA;
                return nhatkychung;
            }
            //create nhatkychung from data
            nhatkychung.SoHieuCT = data.SoCT;
            nhatkychung.NgayThangCT = data.NgayCT;
            nhatkychung.Co = co;
            //check input co or no
            if (co)
            {
                nhatkychung.SoHieuTaiKhoan = data.TaiKhoanCo.MaTaiKhoan;
                nhatkychung.DienGiaiTaiKhoan = data.TaiKhoanCo.Name;
                nhatkychung.SoTienCo = data.SoTien;
            }
            else
            {
                nhatkychung.SoHieuTaiKhoan = data.TaiKhoanNo.MaTaiKhoan;
                nhatkychung.DienGiaiTaiKhoan = data.TaiKhoanNo.Name;
                nhatkychung.SoTienNo = data.SoTien;
            }
            //return nhatkychung
            return nhatkychung;
        }
    }
}
